<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>菜单管理</title>
    <link rel="stylesheet" href="<{$ipath}>alipay/css/base.css">
	 <link rel="stylesheet" href="<{$ipath}>alipay/css/layout.css">
    <link rel="stylesheet" href="<{$ipath}>alipay/css/menu.css">
</head>
<body>
    <div class="query-menu">
        <div class="menu-top">
            <b>菜单管理</b><span>最多可添加4个主菜单，每个主菜单最多可添加5个子菜单。</span>
        </div>
        <div class="menu-left">
           <div class="menu-title">自定义菜单</div>
            <div class="menuContainer">
                <div id="menulistcontainer">

                    <{foreach $menu as $v}>
                        <div class="menu-list">
                            <div class="parent-menu namewrap">
                                <span class="name" val="<{$v.title}>" msgtitle="<{$v.actionParam}>"  contype="<{$v.menuType}>" level="1" contypeval="<{$v.actionParam}>" itemid="<{$v.id}>" pid="0"><{$v.title}>
                                <{if !$v.menuType and !$v.subButton}><b class="notActive"></b><{/if}>
                                </span>

                                <div class="edit"><a class="rename" level="1">编辑</a><a class="del" level="1">删除</a></div></div><div class="submenu-list">

                                <{foreach $v.subButton as $vv}>
                                    <div class="sub-menu-item namewrap">
                                        <span class="name" val="<{$vv.title}>" msgtitle="<{$vv.actionParam}>" itemid="<{$vv.id}>" pid="<{$v.id}>" contype="<{$vv.menuType}>" contypeval="<{$vv.actionParam}>" level="2"><{$vv.title}>
                                            <{if !$vv.menuType}><b class="notActive"></b><{/if}>
                                        </span><div class="edit"> <a class="rename" level="2">编辑</a><a class="del" level="2">删除</a></div>
                                    </div>
                                <{/foreach}>
                                
                                <div class="new-submenu" style="<{if $v.subnum lte 4}>display: block; <{else}>display:none;<{/if}>">+ 添加子菜单</div>
                                
                            </div>
                        </div>
                    <{/foreach}>
                </div>

                <{if $mainnum lte 4}>
                <div class="new-menu">+ 添加主菜单</div>
                <{/if}>
                <div class="saveContainer">
                    <div class="saveTip warn"><i class="iconfont" title="警告"></i><span>当前菜单尚未生效，点击发布后才能在钱包中看到</span></div>
                    <div id="menuSaveBtn" class="btn btn-orange"><a href="javascript:publish();">发 布</a></div>
                </div>

                
            </div>
        </div>
        <div class="menu-right">
            <div class="previewContainer">
                <div class="previewShowBox"></div>
                <div id="previewBtnLists">                    
                </div>
                <div class="previewBottom"> 效果预览 </div>
                <div class="previewtitle">中搜服务</div>
                
            </div>
        </div>

        <div class="editwrap">
            <input maxlength="36" placeholder="主菜单名，最多可输入18个字" class="inputtxt" type="text" />
            <a class="modifyNameAction saveEditName"></a>
            <a class="modifyNameAction cancelEditName">取消</a>
            <div class="editwraptip">超出最大<span></span>字限制</div>
        </div>

        <div class="actionContainer fn-hide notSelectType"><h3 class="title">菜单类型</h3><div class="select-box"><div class="selectInput">请选择类型</div>
            
            <ul class="actionSelect fn-hide" style="display: none;">
             <li val="link" class="deep-back"><span class="typeName link">跳转网页</span><span class="explain">跳转到指定的网页</span></li>
            <li val="in"><span class="typeName in">发送广播</span><span class="explain">接收到一条指定的图文广播</span></li>
            <!--<li val="page" class="deep-back"><span class="typeName page">查看广播正文</span><span class="explain">进入到指定的单图文广播，直接查看正文内容</span></li>-->
            <li val="tel"><span class="typeName tel" >拨打电话</span><span class="explain">拨打指定的电话号码</span></li>
            <li val="map" class="deep-back"><span class="typeName map">查看地图</span><span class="explain">查看指定关键字的地理位置</span></li>
            <!--<li val="transfer"><span class="typeName transfer">转账</span><span class="explain">向指定的已实名认证支付宝账户转账</span></li>
            <li val="record" class="deep-back"><span class="typeName record" >消费记录</span><span class="explain">查看与服务窗之间发生的交易明细</span></li>
            <li val="authlink"><span class="typeName authlink">自动登录</span><span class="explain"><em style="color:red">已弃用</em>，请使用“跳转网页”菜单项替代</span></li>
            <li val="out" class="deep-back"><span class="typeName out">调用服务</span><span class="explain">打开指定的服务和功能(开发者使用)</span></li>-->
            </ul>
            
            </div><div class="container-title"><b>菜单效果</b></div>
        <div class="tplContainer"><div class="container-tip"><i class="iconfont menu-icon vertical-middle" title="提示"></i><span>请先选择菜单类型</span></div></div>
            <div class="inputWrap"></div>
        <div class="btn-wrap"><input class="btn-small btn-orange saveBtn" type="button" value="保存"></div><span class="left-point"></span><a class="cancelBtn">×</a>
		<div class="noset">已有子菜单，无法设置菜单效果。</div>
		</div>

    </div>
    <!-- popup -->
    <div class="ui-mask"></div>
    <div class="alipay-xbox">
        <a class="close" title="关闭本框" href="javascript:;" data-role="close">×</a>
        <div class="alipay-xbox-content"><div class="message-box">
                <div class="title"><h3>选择图文广播</h3></div>
                <div class="top-tools fn-clear">
                    <div class="fl">
                        <a href="?g=alipay&m=fodder&a=single" class="edit-single-message">新建单图文广播</a>
                        <a href="?g=alipay&m=fodder&a=multi" class="edit-multi-message">新建多图文广播</a>
                    </div>
                </div>
                <div class="message-placeholder fn-clear">

                    <{foreach $fodder as $k => $v}>

                    <div class="message-list fl" onclick="choosePopItem(this,<{$k}>)">
                        <div class="message-item">
                            <div class="message-body">
                                <{foreach $v as $kk => $vv}>
                            <input type="hidden" name="sign" id="sign<{$k}>" value="<{$vv.type}>">
                                <a href="" class="item-url">
                                    <{if $kk eq 0}>
                                    <div class="message-title first-message-title"><div class="title-wrapper"><h4 class="popTit"><{$vv.title}></h4><div class="title-bg"></div></div></div>
                                    <div class="message-info"><{$vv.time}></div>
                                    <div class="message-cover-pic">
                                        <img src="<{$vv.img_url}>" class="cover" style="height: 100px;width: 270px;">
                                    </div>
                                    <{else}>
                                    <ul class="show-cont ui-sortable" id="J_showCont">
                                        <li id="item_<{$kk + 1}>" class="show-item fn-clear">
                                            <div class="cover-pic J_change_image" data-image="image" data-default="缩略图">
                                                <img src="<{$vv.img_url}>" class="slImg"/>
                                            </div>
                                            <h1 class="show-title title-break J_change_title" data-title="title" data-default="标题">
                                                <{$vv.title}>
                                            </h1>
                                        </li>
                                    </ul>
                                    <{/if}>
                                    <{if $vv.type eq 1}>
                                    <div class="message-text word-bread"><{$vv.abstract}></div>
                                    <span class="view">立即查看</span>
                                    <{/if}>
                                </a>
                                <{/foreach}>
                            </div>
                            <div class="message-filter" ></div>
                            <!-- 点击确定提交msgid-->
                            <input type="hidden" name="msgid" value="<{$k}>" class="msgid">
                            <div class="chosen" ></div>
                        </div>
                    </div>

                    <{/foreach}>

                </div>
                <div class="actions">
                    <a href="javascript:;" class="submit"><span class="btn btn-gray">确&nbsp;&nbsp;定</span></a>
                    <a href="javascript:;" class="cancel"><span class="btn btn-gray" onclick="closediv('.alipay-xbox','.ui-mask');">取&nbsp;&nbsp;消</span></a>
                </div>
            </div>
        </div>
    </div>
        
<script src="<{$ipath}>public/js/jquery.js" type="text/javascript"></script>
  <script type="text/javascript" src="<{$ipath}>alipay/js/_base.js"></script>
<script type="text/javascript">

           var nopubnum = <{$nopubnum}>;
		  
            //发布
            function publish()
            {
            var mainMenuCount=$('.menuContainer .menu-list').length;
            if(mainMenuCount<=0){
                alert("你还没有添加主菜单，无法发布菜单。");
                return;
            }
             var tosetMenu = $('.menuContainer').find('b.notActive'), tosetMenuLen = tosetMenu.length;
             if(tosetMenuLen>0){
                alert("您有"+tosetMenuLen+"个菜单未设置效果，暂不能发布。");
                return;
             }
				if($("#menuSaveBtn").hasClass("btn-disabled")) return;
                //alert(1);return;
                var url ='?g=alipay&m=menu&a=publish';
                $.post(url, {}, function(data){
                    if(data.status == 1)
                    {
						$("#menuSaveBtn").addClass("btn-disabled").removeClass("btn-orange");
						$(".saveTip.warn").hide();
                        alert('发布成功');
                        //成功后的操作
                    }
                    else
                    {
                        alert('发布失败，你可能打开了对话功能，只能发布3个主菜单，请在支付宝的服务窗平台关闭对话功能');
                    }
                }, 'json');

            }

            /*点击弹层确定*/
            var len = '';
            var sign = '';
            var msgid = '';
            var str = '';
            var title = '';
            var info = '';
            var coverSrc = '';
            var abstract = '';
            var strUl = '';
            var slSrc = '';
            var slTitle = '';
            //$(".xuanqu,.xuanqu-key").click(function(){              
            //    showdiv('.alipay-xbox');				
            //});
            $('.new-text').click(function(event) {
                $(this).css('background', '#ccc');
                $('.message-text-area').show();
                $('.new-start').show()
                $('.message-body-cur').hide();
            });
            $('.key-text').click(function(event) {
                $(this).css('background', '#ccc');
                $('.message-text-area1').show();
                $('.key-start').show()
                $('.message-body-cur1').hide();
            });

            function choosePopItem(obj,n){
                //消息回复和关键词可以共用
                //$(obj).find('.message-filter').css('opacity', '0.3');
				$(obj).addClass("active");
				$(obj).siblings().removeClass("active");
                $(obj).find('.chosen').show();
                //$(obj).siblings().find('.message-filter').css('opacity', '0');
                //$(obj).siblings().find('.chosen').hide();
                title = $(obj).find('.popTit').text();
                info  = $(obj).find('.message-info').text();
                coverSrc = $(obj).find('img.cover').attr('src');
                abstract = $(obj).find('.word-bread').text();
                len = $(obj).find('ul').length;
                sign = $('#sign'+n).val();//判断是单图文还是多图文
                msgid = $(obj).find('.msgid').val();//单击的id
                for (var i = 0; i < len; i++) {
                    slSrc = $(obj).find('.show-cont').eq(i).find('.slImg').attr('src');
                    slTitle = $(obj).find('.show-cont').eq(i).find('.show-title').text();
                    strUl += '<ul class="show-cont ui-sortable" id="J_showCont"><li class="show-item fn-clear"><div class="cover-pic J_change_image" data-image="image" data-default="缩略图"><img src="'+slSrc+'" /></div><h1 class="show-title title-break J_change_title" data-title="title" data-default="标题">'+slTitle+'</h1></li></ul>';
                };
                //alert(sign);
                //alert(msgid);
				$(".alipay-xbox .submit span").removeClass("btn-gray").addClass("btn-orange");
            }

			$(".alipay-xbox .submit").click(function(){
				$(".actionContainer .inputWrap input.typeContentInput").val(msgid);
				closediv(".alipay-xbox",".ui-mask");
				var selectmsg=$(".actionContainer .inputWrap .selectedmsg");
				if(selectmsg.length>0){
					selectmsg.find(".title").text(title);
				}else{
					$(".actionContainer .inputWrap").append("<p class='selectedmsg'><span class='icon'>图文</span><span class='title'>"+title+"</span></p>");
				}
				$(".actionContainer .select-material").text("更改");
			});

</script>

  
    <script>
        $(function () {
			 if(nopubnum>0){
				$(".saveTip.warn").show();
				$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
			 }else{
				 $(".saveTip.warn").hide();
				$("#menuSaveBtn").addClass("btn-disabled").removeClass("btn-orange");
			 }
            hover();
            bindBtn();

            //类型选择
            $(".actionSelect li").click(function () {
                $(".actionSelect").hide();
                var val = $(this).attr("val");
                var text = $(this).find(".typeName").text();                
                $(".actionContainer .selectInput").text(text);   
                $(".actionContainer").attr("contype",val);
                var inputWrap = $(".actionContainer .inputWrap");
                inputWrap.empty();
                if (val == "link") {
                    inputWrap.append('<input id="urladdress" valid-pattern="^(http|https|alipays|juniu):(//).+$" placeholder="请填写以http://或https://开头的网页地址" class="typeContentInput" type="text"><br/><a class="a_choose" onclick="top.showdiv(\'above\')">从功能库添加</a>');
                    $(".actionContainer .container-tip span").text("用户点击该菜单后，将跳转到以下网页地址。");
                }
                if (val == "in") {
                    inputWrap.append('<a  class="select-material">选择图文广播</a><input type="hidden" class="typeContentInput"/>');
                    $(".actionContainer .container-tip span").text("用户点击该菜单后，将收到以下图文广播。");
                    inputWrap.find(".select-material").unbind().click(function(){
                        popup();
                    });
                }
                if (val == "page") {
                    inputWrap.append('<a  class="select-material">选择图文广播</a><input type="hidden" class="typeContentInput"/>');
                    $(".actionContainer .container-tip span").text("用户点击该菜单后，将进入以下图文广播，直接查看正文内容。");
                    inputWrap.find(".select-material").unbind().click(function(){
                        popup();
                    });
                }
                if (val == "tel") {
                    inputWrap.append('<input valid-pattern="^\d+$" placeholder="输入电话号码" type="text" class="typeContentInput">');
                    $(".actionContainer .container-tip span").text("用户点击该菜单后，将拨打以下电话号码。");
                }
                if (val == "map") {
                    inputWrap.append('<input valid-pattern="^\S+$" placeholder="填写关键字，例如“酒店”" type="text" class="typeContentInput">');
                    $(".actionContainer .container-tip span").text("用户点击该菜单后，将查看附近以下关键字的地理位置。示例");
                }
                
            });
            //弹出广播选择框
            function popup(){
                showdiv('.alipay-xbox');
				$(".alipay-xbox .message-list.active").removeClass("active");
            }
            $(".alipay-xbox .close").click(function(){
                closediv(".alipay-xbox",".ui-mask");
            });




            //弹出类型选择列表
            $(".actionContainer .select-box").unbind().toggle(function () {
                $(".actionSelect").show();
            }, function () {
                $(".actionSelect").hide();
            });
            $(".actionContainer .cancelBtn").click(function () {
                $(".actionContainer").hide();
            });
            //类型保存
            $(".actionContainer .saveBtn").unbind().click(function () {
                var actionContainer=$(".actionContainer");
                var itemid=actionContainer.attr("itemid");
                var contype=actionContainer.attr("contype");
                var contypeval=actionContainer.find("input.typeContentInput").val();
                $("span.name[itemid='"+itemid+"']").attr("contype",contype);
                $("span.name[itemid='"+itemid+"']").attr("contypeval",contypeval);
				if(contype=="in"||contype=="page"){
				$("span.name[itemid='"+itemid+"']").attr("msgtitle",title);
				}
				

                var url ='?g=alipay&m=menu&a=addtype';
                $.post(url, {menuType: contype, id: itemid, actionParam: contypeval}, function(data){
                    if(data.status == 1)
                    {
                        //成功后的操作
						$(".actionContainer").hide();
						$("span.name[itemid='"+itemid+"']").find("b").remove();
						$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
						$(".saveTip.warn").show();
                    }
                }, 'json');


            });

            function hover() {
                $(".query-menu .menu-left .parent-menu,.query-menu .menu-left .sub-menu-item").unbind();
                $(".query-menu .menu-left .parent-menu,.query-menu .menu-left .sub-menu-item").hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover") });
            }
            //添加新菜单按钮
            $(".new-menu").unbind().click(function () {
                if ($(".editwrap:visible").length > 0) {
                    $(".editwrap .cancelEditName").click();
                }
                if ($(".new-menu:hidden").length>0) return;
                $(this).hide();
                var container = $("#menulistcontainer");
                var editwrap = $(".editwrap");
                editwrap.attr("edittype", "new");
                editwrap.attr("level", "1");
                editwrap.attr("pid", "0");
                editwrap.show();
                editwrap.find(".inputtxt").attr("placeholder","主菜单名，最多可输入18个字");
                container.append(editwrap);
                $(".actionContainer .cancelBtn").click();
            });
            //编辑菜单按钮
            function bindBtn() {
                $(".menu-list .edit .rename").unbind().click(function () {
                    var editwrap = $(".editwrap");
                    editwrap.find(".cancelEditName").click();
                    editwrap.attr("edittype", "edit");
                    editwrap.attr("level", $(this).attr("level"));
                    editwrap.show();
                    var parent = $(this).parents(".namewrap");
                    parent.find(".name").hide();
                    parent.find(".edit").hide();
                    editwrap.find(".inputtxt").val(parent.find(".name").attr("val"));
                    if(editwrap.attr("level")=="1"){
                    editwrap.find(".inputtxt").attr("placeholder","主菜单名，最多可输入18个字");
                    }else{
                    editwrap.find(".inputtxt").attr("placeholder","子菜单名，最多可输入20个字");
                    }
                    parent.append(editwrap);
                    checkMenuList();
                    $(".actionContainer .cancelBtn").click();
                    
                });
				//删除菜单按钮
                $(".menu-list .edit .del").unbind().click(function () {
                    $("body").append($(".actionContainer"));
                    $(".actionContainer").hide();
                    if ($(this).attr("level") == "1") {
                        if (confirm("确认删除该菜单项以及其下所有子菜单？")) {
                            $(this).parents(".menu-list").remove();
							var name=$(this).parents(".parent-menu").find(".name");
                            var url ='?g=alipay&m=menu&a=delmenu';
                            $.post(url, {id: name.attr("itemid"), level: 1}, function(data){
                                if(data.status == 1)
                                {
                                
                                    //成功后的操作
									$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
									$(".saveTip.warn").show();
									if($(".menu-list").length<4){
										$(".new-menu").show();
									}
                                    previewUpdate();
                                }
                            }, 'json');
                        }
                    }else{
						if (confirm("确认删除该菜单项？")) {
							//$(this).parents(".sub-menu-item").remove();
							var subitem=$(this).parents(".sub-menu-item");
							var name=$(this).parents(".sub-menu-item").find(".name");
                            var url ='?g=alipay&m=menu&a=delmenu';
							var _this=$(this);
                            $.post(url, {id: name.attr("itemid"), level: 2}, function(data){
                                if(data.status == 1)
                                {
								
                                    //成功后的操作
									
									if(subitem.siblings(".sub-menu-item").length==0){
										subitem.parents(".menu-list").find(".parent-menu .name").append('<b class="notActive"></b>');
									}									
									subitem.siblings(".new-submenu").show();
									subitem.remove();
									$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
									$(".saveTip.warn").show();
                                    previewUpdate();

                                }
                            }, 'json');

						}
					}
                    $(".actionContainer .cancelBtn").click();
                });
                //添加二级
                $(".new-submenu").unbind().click(function () {
					var subitems=	$(this).parents(".submenu-list").find(".sub-menu-item");
					var thisitem=$(this).parents(".menu-list").find(".parent-menu .name");
					var contype= thisitem.attr("contype");
					var pid=thisitem.attr("itemid");

					if(subitems.length==0&&contype&&contype!=""){
						if(!confirm("确定要添加子菜单？添加子菜单后，当前设置的菜单效果将会被清除。")){
							return;
						}else{
                            var url ='?g=alipay&m=menu&a=deltype';
                            $.post(url, {id: pid}, function(data){
                                if(data.status == 1)
                                {
                                    //成功后的操作
									thisitem.attr("contype","");
									thisitem.attr("contypeval","");
									thisitem.find("b").remove();
									$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
									$(".saveTip.warn").show();
                                }
                            }, 'json');
                        }
					}

                    if ($(".editwrap:visible").length > 0) {
                        $(".editwrap .cancelEditName").click();
                    }
                    $(this).hide();
                    
                    var container = $(this).parent(".submenu-list");
                    var editwrap = $(".editwrap");
                    editwrap.attr("edittype", "new");
                    editwrap.attr("level", "2");
                    editwrap.attr("pid", pid);
                    editwrap.show();
                    editwrap.find(".inputtxt").attr("placeholder","子菜单名，最多可输入20个字");
                    container.append(editwrap);
                    $(".actionContainer .cancelBtn").click();
                });

                //弹出设置类型框
                $(".menu-list .name").unbind().click(function () {
					

                    $(".actionContainer").show();
                    var namewrap = $(this).parents(".namewrap");
                    namewrap.append($(".actionContainer"));
                    if ($(this).attr("level") == "1") {
                        $(".actionContainer").css({ "left": "290px" });
                    } else {
                        $(".actionContainer").css({"left":"250px"});
                    }
                    var itemid = $(this).attr("itemid")|0;
                    var contype = $(this).attr("contype");
                    var contypeval = $(this).attr("contypeval");
					var msgtitle=$(this).attr("msgtitle");
                    $(".actionContainer").attr("itemid", itemid);
                    $(".actionContainer").attr("contype", contype);
                    $(".actionContainer").attr("contypeval", contypeval);
                    var level = $(this).attr("level");
                    if (contype && contype != "") {
                        var inputWrap = $(".actionContainer .inputWrap");
                        inputWrap.empty();                        
                        if (contype == "link") {
                            $(".actionContainer .selectInput").text("跳转网页");
                            inputWrap.append('<input id="urladdress" valid-pattern="^(http|https|alipays|juniu):(//).+$" placeholder="请填写以http://或https://开头的网页地址" class="typeContentInput" type="text" value="' + contypeval + '"><br/><a class="a_choose" onclick="top.showdiv(\'above\')">从功能库添加</a>');
                            $(".actionContainer .container-tip span").text("用户点击该菜单后，将跳转到以下网页地址。");
                        }
                        if (contype == "in") {
                            $(".actionContainer .selectInput").text("发送广播");
                            inputWrap.append('<a  class="select-material">更改</a><input type="hidden" class="typeContentInput"/><p class="selectedmsg"><span class="icon">图文</span><span class="title">'+msgtitle+'</span></p>');
                            $(".actionContainer .container-tip span").text("用户点击该菜单后，将收到以下图文广播。");
                            inputWrap.find(".select-material").unbind().click(function(){
                                popup();
                            });
                        }
                        if (contype == "page") {
                            $(".actionContainer .selectInput").text("查看广播正文");
                            inputWrap.append('<a  class="select-material">更改</a><input type="hidden" class="typeContentInput"/><p class="selectedmsg"><span class="icon">图文</span><span class="title">'+msgtitle+'</span></p>');
                            $(".actionContainer .container-tip span").text("用户点击该菜单后，将进入以下图文广播，直接查看正文内容。");
                            inputWrap.find(".select-material").unbind().click(function(){
                                popup();
                            });
                        }
                        if (contype == "tel") {
                            $(".actionContainer .selectInput").text("拨打电话");
                            inputWrap.append('<input valid-pattern="^\d+$" placeholder="输入电话号码" type="text" class="typeContentInput" value="' + contypeval + '">');
                            $(".actionContainer .container-tip span").text("用户点击该菜单后，将拨打以下电话号码。");
                        }
                        if (contype == "map") {
                            $(".actionContainer .selectInput").text("查看地图");
                            inputWrap.append('<input valid-pattern="^\S+$" placeholder="填写关键字，例如“酒店”" type="text" class="typeContentInput" value="' + contypeval + '">');
                            $(".actionContainer .container-tip span").text("用户点击该菜单后，将查看附近以下关键字的地理位置。示例");
                        }
                    } else {
                        $(".actionContainer .selectInput").text("请选择类型");
                        $(".actionContainer .inputWrap").empty();
                        $(".actionContainer .container-tip span").text("请先选择菜单类型");
                    }

					if($(this).parents(".menu-list").find(".submenu-list .sub-menu-item").length>0&&$(this).attr("level")=="1"){
						$(".actionContainer .noset").show();
					}else{
						$(".actionContainer .noset").hide();
					}
                });
            }
            
            //确定添加、编辑
            $(".editwrap .saveEditName").unbind().click(function () {                
                var text = $.trim($(".editwrap .inputtxt").val());
                if (text == "") {
                    $(".editwrap .cancelEditName").click();
                }
                
                //一级添加
                if ($(".editwrap").attr("edittype") == "new" && $(".editwrap").attr("level") == "1") {
                    if (getByteLength(text) > 18) {
                        $(".editwrap .editwraptip").show();
                        $(".editwrap .editwraptip span").text(18);
                        $(".editwrap .editwraptip").fadeOut(3000);
                        return;
                    }
                    var container = $("#menulistcontainer");
                    var menuitem = $('<div class="menu-list">' +
                        '<div class="parent-menu namewrap">' +
                            '<span class="name" val="' + text + '" contype="" level="1" contypeval="" itemid="0" pid="'+$(".editwrap").attr("pid")+'">' + text + '<b class="notActive"></b></span>' +
                            '<div class="edit">' +
                                '<a class="rename" level="1">编辑</a>' +
                                '<a class="del" level="1">删除</a>' +
                            '</div>' +
                        '</div>' +
                        '<div class="submenu-list">' +
                            '<div class="new-submenu">+ 添加子菜单</div>' +
                        '</div>' +
                    '</div>');
                    container.append(menuitem);
                    hover();
                    
                    bindBtn();
                   
                    $(".editwrap .cancelEditName").click();

                    var url ='?g=alipay&m=menu&a=addmenu';
                    $.post(url, {title: text, pid: 0, level: 1}, function(data){
                        if(data.status == 1)
                        {
                            //添加成功后，返回菜单的id
                            //alert(data.id);
                            menuitem.find(".name").attr("itemid",data.id);
							 menuitem.find(".name").click();
							 $("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
							 $(".saveTip.warn").show();
                        }
                    }, 'json');
                }
                //编辑
                if ($(".editwrap").attr("edittype") == "edit") {
                    var level = $(".editwrap").attr("level");
                    var parent = $(this).parents(".namewrap");
                    if (parent.length > 0) {
                        var name = parent.find(".name");
                        var maxcount = 18;
                        if (level == "2") {
                            maxcount = 20;
                        }
                        if (getByteLength(text) > maxcount) {
                            $(".editwrap .editwraptip").show();
                            $(".editwrap .editwraptip span").text(maxcount);
                            $(".editwrap .editwraptip").fadeOut(3000);                            
                            return;
                        }
                        var notActive = text;
                        if (name.attr("contype") == undefined || name.attr("contype") == "") {
                            notActive += '<b class="notActive"></b></span>';
                        }
                        name.attr("val", text).html(notActive);

                        var url ='?g=alipay&m=menu&a=editmenu';
                        $.post(url, {title: text, id: name.attr("itemid")}, function(data){
                            if(data.status == 1)
                            {
                                //添加成功后，返回菜单的id
								$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
								$(".saveTip.warn").show();

                            }
                        }, 'json');

                    }
                    $(".editwrap .cancelEditName").click();
                }
                //二级添加
                if ($(".editwrap").attr("edittype") == "new" && $(".editwrap").attr("level") == "2") {
                    if (getByteLength(text) > 20) {
                        $(".editwrap .editwraptip").show();
                        $(".editwrap .editwraptip span").text(20);
                        $(".editwrap .editwraptip").fadeOut(3000);
                        return;
                    }
                    var text = $.trim($(".editwrap .inputtxt").val());
                    if (text == "") {
                        $(".editwrap .cancelEditName").click();
                    }
                    var container = $(this).parents(".submenu-list");
                    var newsubmenu = container.find(".new-submenu");
                    var pid=$(".editwrap").attr("pid");
                    var menuitem = $('<div class="sub-menu-item namewrap">' +
                                '<span class="name" val="' + text + '" contype=""  level="2" contypeval="" itemid="0" pid="' + pid + '">' + text + '<b class="notActive"></b></span>' +
                                '<div class="edit">' +
                                   ' <a class="rename" level="2">编辑</a>' +
                                    '<a class="del" level="2">删除</a>' +
                                    '</div>' +
                            '</div>');
                    newsubmenu.before(menuitem);
                    hover();
                    bindBtn();
                    
                    $(".editwrap .cancelEditName").click();
                    var url ='?g=alipay&m=menu&a=addmenu';
                    $.post(url, {title: text, pid: pid, level: 2}, function(data){
                        if(data.status == 1)
                        {
                            //添加成功后，返回菜单的id
                            //alert(data.id);
                            menuitem.find(".name").attr("itemid",data.id);
							menuitem.find(".name").click();
							$("#menuSaveBtn").removeClass("btn-disabled").addClass("btn-orange");
							$(".saveTip.warn").show();
                           var menu_list= container.parents(".menu-list");
                           menu_list.find(".parent-menu .name .notActive").remove();
                        }
                    }, 'json');
                }
                previewUpdate();
            });
            $(".editwrap .cancelEditName").unbind().click(function () {
                var parent = $(this).parents(".namewrap");
                if (parent.length > 0) {
                    parent.find(".name").show();
                    parent.find(".edit").show();
                }
                if ($(this).parents(".submenu-list").find(".sub-menu-item").length < 5) {
                    $(this).parents(".submenu-list").find(".new-submenu").show();
                }
                cancel();
                
            });
            function cancel() {
                
                $(".editwrap").removeAttr("edittype");                
                $(".editwrap .inputtxt").val("");
                $(".new-menu").show();
                $(".query-menu").append($(".editwrap").hide());
                checkMenuList();
            }
            checkMenuList();
            function checkMenuList() {
                var count = $(".menu-list").length;
                if (count >= 4) {
                    $(".new-menu").hide();
                } else {
                    $(".new-menu").show();
                }
            }
			previewUpdate();
            function previewUpdate() {
                var previewBtnLists = $("#previewBtnLists");
                previewBtnLists.empty();
                var menus = $(".menu-list");
                var w = 100 / menus.length;
                menus.each(function () {
                    var item = $('<div class="preview-items" style="width: ' + w + '%;"><a class="menu menu-action ellipsis" >' + $(this).find(".name").attr("val") + '</a></div>');
                    previewBtnLists.append(item);
                    var submenus= $(this).find(".submenu-list .sub-menu-item");
                    if (submenus.length > 0) {
                        item.find("a").addClass("hasSubMenu");
                        var ul = $('<ul class="preview-sub-items"></ul>');
                        item.append(ul);
                        submenus.each(function () {
                            ul.append('<li class="ellipsis">' + $(this).find(".name").attr("val") + '</li>');
                        });
                        ul.append('<li class="btm-point"></li>');
                    }
                                    
                });
                $(".preview-items .hasSubMenu").unbind().toggle(function () {
                    $("#previewBtnLists ul").hide();
                    $(this).next("ul").show();
                }, function () {
                    $(this).next("ul").hide();
                });
            }
        });
        function getByteLength(str) {
            return str.replace(/[^\x00-\xff]/g, "**").length;
        }
    </script>
</body>
</html>
